import list from '../Api/index.js'
const app = getApp();
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const getNowDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth()
  const day = date.getDate()
  const weekArr = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
  return {
    year: year,
    month: weekArr[month],
    day:formatNumber(day)
  }
}

const MathNum = () =>{
  let date = new Date()
  let day = date.getDate() - 1
  return list.data.list[day]
}

const cunTimetype = () =>{
  let date = getNowDate(new Date())
  let NewData = date.day
  let dateInfo = {
    time: NewData,
    type: 0
  }
  let dateTime = wx.getStorageSync('date');
  if (dateTime) {
    if (NewData != dateTime.time) {
      wx.setStorageSync('date', dateInfo)
      wx.setStorageSync('indexdate', MathNum())
    }
  } else {
    wx.setStorageSync('date', dateInfo)
    wx.setStorageSync('indexdate', MathNum())
  }
}

const signSucceed = (num) =>{
  let date = getNowDate(new Date())
  let NewData = date.day
  let dateInfo = {
    time: NewData,
    type: 1
  }
  wx.setStorageSync('date', dateInfo)
  let score = wx.getStorageSync('score');
  if(score){
    wx.setStorageSync('score', score + num )
  }else{
    wx.setStorageSync('score', num)
  }
}



const signListTap = (text) => {
  let data = {
    time: formatTime(new Date()),
    title: text
  }
  let dynamic = wx.getStorageSync('dynamic') || []
  dynamic.unshift(data)
  wx.setStorageSync('dynamic', dynamic)
}

const getSystemInfo = () =>{
  let data
  wx.getSystemInfo({
    success: res=> {
      res.system = res.system.split(" ")[0]
      data = res
    }
  })
  return data
}

function Get(url, data, cb) {
  wx.request({
    url: app.path.HTTP + url + '?appkey=' + app.path.appkey,
    data: data,
    method: 'get',
    header: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
    success: res => {
      return typeof cb == "function" && cb(res.data)
    },
    fail: ()=> {
      console.log("调用失败")
      return typeof cb == "function" && cb(false)
    }
  })
}  

module.exports = {
  formatTime,
  Get,
  getNowDate,
  MathNum,
  cunTimetype,
  signSucceed,
  signListTap,
  getSystemInfo
}
